<?php

if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('htfrmmanagerhotel_loadMenu');      
$objXajax->registerFunction('htfrmmanagerhotel_loadGrid');      
$objXajax->registerFunction('htfrmmanagerhotel_checkedRoom');
$objXajax->registerFunction('htfrmmanagerhotel_addRemoveProduct');
$objXajax->registerFunction('htfrmmanagerhotel_moveRoom');
$objXajax->registerFunction('htfrmmanagerhotel_loadProduct');
$objXajax->registerFunction('htfrmmanagerhotel_finishRoom');
$objXajax->registerFunction('htfrmmanagerhotel_addNewProduct');
$objXajax->registerFunction('htfrmmanagerhotel_ajxTotalRoom');
$objXajax->registerFunction('htfrmmanagerhotel_undo');
$objXajax->registerFunction('htfrmmanagerhotel_loadRoom');
$objXajax->registerFunction('htfrmmanagerhotel_rentRoom');
$objXajax->registerFunction('htfrmmanagerhotel_showBill');
$objXajax->registerFunction('htfrmmanagerhotel_saveCustomer');
$objXajax->registerFunction('htfrmmanagerhotel_deleteCustomer');
/*
* Hàm dùng để load sản phâm cho left menu 
* 
*/
function htfrmmanagerhotel_loadMenu(){     
    global $lang,$arrMenu,$arrdetail;
    $objResponse = new xajaxResponse();
    $arr = array();
    $arrMain = htfrmmanagerhotel_getMenu($arr,$arrdetail); 
      
    $oTbs = new clsTinyButStrong();
    $oTbs ->LoadTemplate("../templates/$lang/$path/ht_managerhotel_menu_grid.html");
    $oTbs->MergeBlock('blk_list', $arrMain);
    $oTbs->MergeBlock('blk_list1', 'array', 'arrdetail[%p1%]');
    $objResponse->addAssign('htfrmmanagerhotel_leftmenu', 'innerHTML', $oTbs->Source);
     
    $count = count($arr);
    for($i=0;$i<$count;$i++){ // tạo mãng tên và giá cho sản phẩm
        $name     = $arr[$i]['name'];
        $price    = $arr[$i]['price'];
        $id       = $arr[$i]['id'];
        $unit     = $arr[$i]['unit'];
        $unitname = $arr[$i]['unitname'];
        $objResponse->addScript("ht_obj.createAssignProductPrice($id,'$name','$price','$unit','$unitname');"); 
    }
    $indexNewProduct = time();
    $objResponse->addScript("ht_obj.indexNewProduct=$indexNewProduct;");                            
    $objResponse->addScript("htfrmmanagerhotel_dropdrapmenu();");                           
    $objResponse->addScript('$("div.ht_divProduct").droppable("disable");'); 
    
    return $objResponse->getXML();
}

function htfrmmanagerhotel_loadRoom(){ 
    global $lang,$arrDetail;
        $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
        $arrDetail = htfrmmanagerhotel_getRoomInit();
        for($i=0;$i<count($arrDetail);$i++){
            $id = $arrDetail[$i]['id'];
            $name = $arrDetail[$i]['name'];
            $type = $arrDetail[$i]['type_id'];            
            $timestamp = $arrDetail[$i]['timestamp'];
            $current_id = $arrDetail[$i]['current_id']==''?0:$arrDetail[$i]['current_id'];
            $status = $timestamp>0?1:0;
            if($status==1){
                $objResponse->addScript("htfrmmanagerhotel_ActiveStatus($id);"); 
            }
            $objResponse->addScript("ht_obj.createAssignRoomObj($id,'$name',$type,$status,$timestamp,$current_id);"); 
        }
        $objResponse->addScript('htAssignTime();');
        
        $objResponse->addScript("ht_obj.initSetCustomer();"); 
        $arrCustomer = htfrmmanagerhotel_getCustomer(); 
       // echo '<pre>'; print_r($arrCustomer);die();
        for($i=0;$i<count($arrCustomer);$i++){
            $name = $arrCustomer[$i]['name'];
            $cmnd = $arrCustomer[$i]['cmnd'];
            $age = $arrCustomer[$i]['age'];
            $sex = $arrCustomer[$i]['sex'];
            $moto = $arrCustomer[$i]['moto'];
            $address = $arrCustomer[$i]['address'];
            $generate_id = $arrCustomer[$i]['generate_id'];
            $room_id = $arrCustomer[$i]['room_id'];
            $dum_id = $arrCustomer[$i]['dum_id'];
            $objResponse->addScript("ht_obj.assignOldCustomer('$room_id','$dum_id','$name','$cmnd','$age','$address','$sex','$moto');");  
        }
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function htfrmmanagerhotel_loadGrid(){     

    global $lang,$arrMenu,$arrDetail,$objDbSelect,$tbl_postfix,$lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrDetail = array();
    $arrRoom = htfrmmanagerhotel_getRoom($arrDetail);   

    $oTbs = new clsTinyButStrong();  
    
    // thêm vào phần cấu hình tín tiền phòng    
    $sqlroomtype = "select *,name_$lang as name from ht_room_type$tbl_postfix ";
    //echo $sqlroomtype; die();
    $arrRoomType = $objDbSelect->GetArray($sqlroomtype); 
    for($i=0;$i<count($arrRoomType);$i++){
       $id=$arrRoomType[$i]['id'] ;
       $name=$arrRoomType[$i]['name'] ;
       $color=$arrRoomType[$i]['style'] ;
       $priceone=$arrRoomType[$i]['priceone'] ;
       $pricetwo=$arrRoomType[$i]['pricetwo'] ;
       $pricethree=$arrRoomType[$i]['pricethree'] ;
       $pricefour=$arrRoomType[$i]['pricefour'] ;
       $pricefive=$arrRoomType[$i]['pricefive'] ;
       $priceinday=$arrRoomType[$i]['priceinday'] ;
       $objResponse->addScript("ht_obj.createAssignRoomPrice('$id','$name','$priceone','$pricetwo','$pricethree','$pricefour','$pricefive','$priceinday','$color');"); 
    }
    $sqlconfig = "Select * from cus_config$tbl_postfix "; 
    $arrConfig = $objDbSelect->GetArray($sqlconfig);    
    if(is_array($arrConfig)&& count($arrConfig)>0){
        $saturdayadd = $arrConfig[0]['fee_saturday'];
        $sundayadd = $arrConfig[0]['fee_sunday'];
        $starttime = $arrConfig[0]['starttime'];
        $endtime = $arrConfig[0]['endtime'];
        $objResponse->addScript("ht_obj.setaddDate($saturdayadd,$sundayadd);"); 
        $objResponse->addScript("ht_obj.setTimeRoom($starttime,$endtime);"); 
    }
    
    // thêm vào phần cấu hình tín tiền phòng
      
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/ht_managerhotel_grid.html');   
    $oTbs->MergeBlock('blk_total', $arrRoom); 
    $oTbs->MergeBlock('blk_data', 'array', 'arrDetail[%p1%]');
   // $oTbs->MergeBlock('blk_data', $arrRoom);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('htfrmmanagerhotel_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScript("addJsContext();"); 
            
   $objResponse->addScript('$(".clsbtn_ht").button();');          
   $objResponse->addScript('$(".chkHide").button();');          
   $objResponse->addScript('xajax_htfrmmanagerhotel_loadMenu();');           
   $objResponse->addScript('xajax_htfrmmanagerhotel_loadRoom();');          
   $objResponse->addScript('xajax_htfrmmanagerhotel_loadProduct();');          
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function htfrmmanagerhotel_getCustomer(){
     global $objDbSelect,$tbl_postfix;
    $sqlSelect=" SELECT br.room_id,br.id,c.*  FROM ht_room$tbl_postfix as r join ht_room_type$tbl_postfix  as t on r.room_type=t.id left 
     join ht_current_book$tbl_postfix br  on br.room_id = r.id join ht_customer$tbl_postfix c 
     on br.id = c.generate_id  order by c.dum_id" ; 
     //echo $sqlSelect; die();
    return $objDbSelect->GetArray($sqlSelect); 
}

function htfrmmanagerhotel_saveCustomer($current_id,$name,$cmnd,$age,$address,$sex,$moto,$indexNewProduct,$flg=0){
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse = new xajaxResponse();
    $name = addslashes($name);
    $address = addslashes($address); 
    $sql ='';
    if($flg==0){
        $sql = " insert into ht_customer$tbl_postfix(generate_id,name,cmnd,sex,address,age,moto,dum_id)
         value('$current_id','$name','$cmnd','$sex','$address','$age','$moto','$indexNewProduct') " ;
    }else{
       $sql = "UPDATE ht_customer$tbl_postfix SET name='$name',cmnd='$cmnd',age='$age',sex='$sex',address='$address',moto='$moto'
       WHERE generate_id ='$current_id' and dum_id='$indexNewProduct'"; 
    }
    $objDbSelect->Execute($sql);
   // echo $sql;die();
    return $objResponse->getXML();    
}

function htfrmmanagerhotel_deleteCustomer($generate_id,$dum_id){
    global $objDbSelect,$tbl_postfix;
    //$tbl_postfix = $_SESSION['current']['tbl_postfix'];
    $sql = "delete from ht_customer$tbl_postfix where generate_id = '$generate_id' and dum_id ='$dum_id' ";
    $objDbSelect->Execute($sql);
    $objResponse = new xajaxResponse();
    return $objResponse->getXML();    
}

function  htfrmmanagerhotel_getRoom(&$arrDetail){
    global $objDbSelect,$tbl_postfix,$lang;
    $arrResult = array();
    $sqlSelect = "SELECT r.id,r.room_type,r.name,t.style as color,t.name_$lang  as name_type,t.id as type_id
    FROM ht_room$tbl_postfix as r join ht_room_type$tbl_postfix  as t on r.room_type=t.id  order by r.room_type,r.name";
    $arr =  $objDbSelect->GetArray($sqlSelect);
    for($i=0;$i<count($arr);$i++){
        $type_id = $arr[$i]['type_id'];
        $arrResult[$type_id]= array('type_id'=>$arr[$i]['type_id'],'name_type'=>$arr[$i]['name_type']);
        $arrDetail[$type_id][]=array('id'=>$arr[$i]['id'],'color'=>$arr[$i]['color'],'name'=>$arr[$i]['name']);
    }
    return $arrResult;
}
/*
*hàm này dùng để lấy danh sách bảng lên để lưu vào mãng 
* 
*/
function  htfrmmanagerhotel_getRoomInit(){
    global $objDbSelect,$tbl_postfix,$lang;
    $arrResult = array();
    $sqlSelect = "SELECT r.id,r.room_type,r.name,t.style ,t.name_$lang  as name_type,t.id as type_id,c.id as current_id,if(c.timestart is not null or c.timestart <> '',TIMESTAMPDIFF(SECOND,c.timestart,now()),0) as timestamp
    FROM ht_room$tbl_postfix as r join ht_room_type$tbl_postfix  as t on r.room_type=t.id left join 
    ht_current_book$tbl_postfix as c on c.room_id = r.id and c.flgcal = 0 order by r.room_type,r.name";
    $arr =  $objDbSelect->GetArray($sqlSelect);
    return $arr;
}
    

function htfrmmanagerhotel_getMenu(&$arr,&$arrDetail){
    global $objDbSelect,$lang,$tbl_postfix;    
    $sql = "select p1.name_$lang as name,p1.price as price,p1.unit,p1.unitname,p1.id,p1.product_type ,p2.name_$lang as parentname ,p2.icon,p1.parent_id
        from ht_product$tbl_postfix as p1 join ht_product$tbl_postfix as p2 on p1.parent_id
    = p2.id and p2.active = 1 where  p1.active = 1 order by p1.parent_id,p1.product_type " ;
    $arr = $objDbSelect->GetArray($sql);
    $count = count($arr);
    $arrResult = array();
    $index =-1;  
    $strresult='';
    $arrResult = array();
    for($i=0;$i<$count;$i++){
        $name = $arr[$i]['name'];
        $price = $arr[$i]['price'];
        if($arr[$i]['product_type']==0){
            $arrResult[$arr[$i]['parent_id']] = array('name'=>$name);
        }else{
            $arrDetail[$arr[$i]['parent_id']][]=array('name'=>$name,'id'=>$arr[$i]['id'],'price'=>$price);
        }
    }
    return $arrResult;
}
                                           
function htfrmmanagerhotel_addRemoveProduct($arrAddRemove,$arrProduct=''){
     global $lang,$arrMenu,$objDbSelect;
     $objResponse = new xajaxResponse();
    // echo '<pre>'; print_r($arrAddRemove);die();
     for($i=0;$i<count($arrAddRemove);$i++){
         $temp = $arrAddRemove[$i];
         $arrId = explode(';',$temp);
         $product_id = $arrId[0];
         $nump = $arrId[2];
         $general_id = $arrId[1];
         htfrmmanagerhotel_actionDB($general_id,$product_id,$nump,$arrProduct) ;
     }    
    
     return $objResponse->getXML();    
}

function htfrmmanagerhotel_actionDB($general_id,$product_id,$nump,$arrProduct){
  global $objDbSelect,$tbl_postfix,$user_id;
       $sqlSelect = "select num from ht_history_product$tbl_postfix where general_id =  
     '$general_id' and product_id = '$product_id'";
     $arrSelect = $objDbSelect->GetArray($sqlSelect);
     $sql ='';
     if(is_array($arrSelect) && count($arrSelect)==1){
         $curnum =  $arrSelect[0]['num'];// số sản phẩm đã có hiện tại
         $nextnum = $curnum + $nump;
         if($nextnum<1){
             $sql= "delete from ht_history_product$tbl_postfix where general_id =  
     'general_id' and product_id = '$product_id'  ";     
     // write log........................
         }else{
            $sql = "update ht_history_product$tbl_postfix SET num = '$nextnum' where general_id =  
     '$general_id' and product_id = '$product_id' ";     
      // write log........................ 
         }
     }else{
        $name = $arrProduct[$product_id]['name'];
        $price = $arrProduct[$product_id]['price'];
        $sql = "insert into ht_history_product$tbl_postfix(general_id,product_id,name,price,date_created,user_created,num) 
        values('$general_id','$product_id','$name','$price',now(),'$user_id',$nump)"; 
       // write log........................   
     }    
     $objDbSelect->Execute($sql);  
}

function htfrmmanagerhotel_moveRoom($RoomFrom,$RoomTo,$typeFrom,$typeTo){
    global $objDbSelect,$tbl_postfix,$user_id;
    $objResponse = new xajaxResponse();
    $update = '';
    /* phần này dùng để tính riêng khi khách hàng chuyển phòng khách loại. tạm thời chưa cần dùng đến
   // if($typeFrom!=$typeTo){
//        $update = ",timestart = now()";
//        $sqlselect = " select id as general_id,timestart as timefrom,now() as timeto, TIMESTAMPDIFF(MINUTE,timestart,now()) as renttime from ht_current_book$tbl_postfix where room_id = '$RoomFrom' and flgcal = 0 ";
//        $arr =  $objDbSelect->GetArray($sqlselect);
//        $timefrom  = $arr[0]['timefrom'];
//        $timeto    = $arr[0]['timeto'];
//        $general_id    = $arr[0]['general_id'];
//        $renttime    = $arr[0]['renttime'];
//        $price     = ht_calTotal($timefrom,$timeto,$typeFrom);
//        $sql = " insert into tbl_history_move$tbl_postfix(general_id, room_id, user_created, date_created, price, timestart,renttime)
//        values($general_id,$RoomFrom,$user_id,now(),'$price','$timefrom',$renttime) ";
//        $objDbSelect->Execute($sql); 
//    }*/
    $sql = " update ht_current_book$tbl_postfix set room_id = $RoomTo ,user_update = '$user_id', date_update = now() $update where room_id  = $RoomFrom and flgcal = 0 ";

    $objDbSelect->Execute($sql);     
    $objResponse->addScript("ht_calActiveTableByTableType();");
    return $objResponse->getXML();  
}
/*
* Hàm này dùng để load product lên các bàn 
* 
* 
*/
function htfrmmanagerhotel_loadProduct(){
    global $objDbSelect,$tbl_postfix;
     $objResponse = new xajaxResponse();
    $sql = "select c.room_id,p.name,p.price,p.num,p.product_id from ht_history_product$tbl_postfix as p
JOIN ht_current_book$tbl_postfix as c ON c.id = p.general_id and c.flgcal = 0 order by p.date_created"; // lấy các sản phẩm có trên bàn từ DB
    $arr = $objDbSelect->GetArray($sql);
   // $objResponse->addScript("htfrmmanagerhotel_dropdrapmenu();");
    for($i=0;$i<count($arr);$i++){
        $room_id = $arr[$i]['room_id'];
        $name = $arr[$i]['name'];
        $num = $arr[$i]['num'];
        $price = $arr[$i]['price'];
        $product_id = $arr[$i]['product_id'];
        $objResponse->addScript("htfrmmanagerhotel_loadProduct('$room_id','$product_id','$name','$num','$price');"); // load các sản phẩm đó lên bàn
    }
  //  

    $objResponse->addScript("ht_calActiveTableByTableType();");
    $objResponse->addScript("ht_addJsContext();");
   // 
    
   return $objResponse->getXML();  
}
/*
* hàm này dùng để tính toán khi khách hàn kêu tính tiền  
* @room_id : id của bàn cần tính 
* $current_id : id đi của bàn mướn hiện hành
* $pricehour : tiền giờ
* $pricetotal : toàn bộ tiền của phòng
*/
function htfrmmanagerhotel_finishRoom($current_id,$pricehour,$pricetotal,$room_id){    
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse = new xajaxResponse();
    $priceproduct =$pricetotal-$pricehour ;
    $sql = " update ht_current_book$tbl_postfix SET timeend=now(),flgcal =1,money_rent='$pricehour',total='$pricetotal',user_update = $user_id, date_update = now() where id = $current_id";
    if($objDbSelect->Execute($sql)){
        $objResponse->addScript("$('#htfrmmanagerhotel_divprint').dialog('close');");
        $objResponse->addScript("htfrmmanagerhotel_DeactiveStatus($room_id);");
        $objResponse->addScript("ht_obj.resetRoom($room_id);");
        $objResponse->addScript("$('#htfrmmanagerhotel_div_$room_id').find('.trProduct').remove();");
    }
        $objResponse->addScript("ht_calActiveTableByTableType();");
    return $objResponse->getXML();
}
/*
* Hàm này dùng để add sản phẩm ngoài doanh mục 
* $proId : lấy id từ indexproduct
* $hdtRoom : id của bàn thêm sản phẩm mới
* $name : tên sản phẩm 
* $price : giá của sản phẩm ngoài doanh mục
*/
function htfrmmanagerhotel_addNewProduct($proId,$hdRoom,$name,$price,$general_id=''){
    global $objDbSelect,$user_id,$hotel_id,$tbl_postfix;
    $objResponse = new xajaxResponse();
    $sql =''; 
   
    if($general_id ==''){
        $sqlsel = "select id from ht_current_book$tbl_postfix where room_id = '$hdRoom' and flgcal = 0 " ;
        $arr = $objDbSelect->GetArray($sqlsel);
        $general_id = $arr[0]['id'];
    }
    $sql = "insert into ht_history_product$tbl_postfix(product_id,name,price,date_created,user_created,num,general_id) 
    values('$proId','$name','$price',now(),'$user_id',1,'$general_id')";  
     $objDbSelect->Execute($sql);  
    return $objResponse->getXML();    
}

function htfrmmanagerhotel_undo($room_id){
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse = new xajaxResponse();
    
    $sqlgeneral = " select max(id) as general_id,TIMESTAMPDIFF(SECOND,max(timestart),now()) as renttime 
    from ht_current_book$tbl_postfix where room_id=$room_id group by room_id ";
    $arrgeneral = $objDbSelect->GetArray($sqlgeneral);
    $timestamp = $arrgeneral[0]['renttime'];
    //echo $timestamp;die();
    $general_id = $arrgeneral[0]['general_id'];
    
    $sql = "select * from ht_history_product$tbl_postfix where  general_id = $general_id order by date_created"; // lấy các sản phẩm có trên bàn từ DB   
    $objResponse->addScript("ht_obj.arrRoom[$room_id].status=1;"); 
    $objResponse->addScript("ht_obj.arrRoom[$room_id].timestamp=$timestamp;"); 
    $objResponse->addScript("ht_obj.arrRoom[$room_id].current_id=$general_id;"); 
   
    $arr = $objDbSelect->GetArray($sql);
    $sqlUpdate = "update ht_current_book$tbl_postfix set flgcal = 0,timeend=null,money_rent=0,total=0 where id = $general_id ";
    $objDbSelect->Execute($sqlUpdate);
    if(is_array($arr) && count($arr)>0){
      for($i=0;$i<count($arr);$i++){
        $name = $arr[$i]['name'];
        $num = $arr[$i]['num'];
        $price = $arr[$i]['price'];
        $product_id = $arr[$i]['product_id'];
        $objResponse->addScript("htfrmmanagerhotel_loadProduct('$room_id','$product_id','$name','$num','$price');"); // load các sản phẩm đó lên bàn  
     }  
    }
    //$objResponse->addScript("htcalActiveRoomByRoomType();"); 
    $objResponse->addScript("htfrmmanagerhotel_ActiveStatus($room_id)");
    $objResponse->addScript("ht_calActiveTableByTableType();");
    return $objResponse->getXML();    
}

function htfrmmanagerhotel_rentRoom($roomId){
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse = new xajaxResponse();
    $sql = "INSERT into ht_current_book$tbl_postfix(timestart, room_id, first_room, user_created, date_created) VALUE(now(),'$roomId','$roomId',$user_id,now())";    
    $objDbSelect->Execute($sql);
    $current = mysql_insert_id();    
    $objResponse->addScript("ht_obj.AssignStatusRoom($roomId,1,$current);"); 
    $objResponse->addScript("htfrmmanagerhotel_ActiveStatus($roomId)");
    $objResponse->addScript("ht_calActiveTableByTableType();"); 
    return $objResponse->getXML(); 
}

//function htfrmmanagerhotel_showBill($current_id,$Room_id,$arrRoomPrice,$startTime,$endTime,$flgCal){
function htfrmmanagerhotel_showBill($current_id,$Room_id,$arrRoomPrice,$startTime,$endTime,$flgCal){
    global $objDbSelect,$user_id,$tbl_postfix,$hourPay,$hourmoney,$total,$lang,$path;
    $objResponse = new xajaxResponse();
   //echo '<pre>'; print_r($arrRoomPrice); die();
//    $sqlhour = "select c.timestart,now() as timeend,r.room_type,TIMESTAMPDIFF(SECOND,timestart,now()) as renttime from ht_current_book$tbl_postfix c join ht_room$tbl_postfix r on c.room_id = r.id where c.id = '$current_id'";
   // echo 
//    $arrhour = $objDbSelect->GetArray($sqlhour);
//    $from = $arrhour[0]['timestart'];
//    $to = $arrhour[0]['timeend'];
//    $room_type_id = $arrhour[0]['room_type'];
//    $renttime = $arrhour[0]['renttime'];
//    $hourPay=ConvertToHM($renttime);    
    //$hourmoney = ht_calTotal($from,$to,$room_type_id);
          $sqlS = " SELECT c.id,c.timestart as timeStart,now() as timeEnd,(UNIX_TIMESTAMP(now())-UNIX_TIMESTAMP(c.timestart))/3600 as timeTotal,TIMESTAMPDIFF(SECOND,timestart,now()) as renttime FROM ht_current_book$tbl_postfix c  WHERE c.id = '$current_id' "; 
          $rs = $objDbSelect->GetArray($sqlS); 
    $renttime = $rs[0]['renttime'];
    $hourPay=ConvertToHM($renttime);  
           //echo $sqlS; die();
    $hourmoney = htfrmmanagerhotel_calRentRoom($rs,$arrRoomPrice,$startTime,$endTime,$flgCal);
    //echo "ht_calTotal($from,$to,$room_type_id)";die();
    
    $sql="SELECT  name, num, price * num as total,0 as style FROM ht_history_product$tbl_postfix where general_id = '$current_id' ";
    $arr = $objDbSelect->GetArray($sql);
    $counttemp = count($arr);
    $sqlHistorymove = " select r.name, h.renttime,h.price,1 as style from ht_history_move$tbl_postfix as h join ht_room$tbl_postfix as r ON r.id = h.room_id where general_id = '$current_id' ";
    $arrhistory = $objDbSelect->GetArray($sqlHistorymove);
    //echo $sqlHistorymove;die();
    for($i=0;$i<count($arrhistory);$i++){
       $arr[$i+$counttemp]['name'] ='Tiền giờ bàn '. $arrhistory[$i]['name'];
       $arr[$i+$counttemp]['num'] = ConvertToHM($arrhistory[$i]['renttime']);
       $arr[$i+$counttemp]['total'] = $arrhistory[$i]['price'];
       $arr[$i+$counttemp]['style'] = $arrhistory[$i]['style'];
    }
    $priceproduct = 0;
    for($i=0;$i<count($arr);$i++){
        $priceproduct += (int)$arr[$i]['total'];
    }
    //echo '<pre>'; print_r();die();
    $total=$hourmoney+$priceproduct;
    $oTbs = new clsTinyButStrong();    
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/ht_managerhotel_bill.html');   
    $oTbs->MergeBlock('blk_list', $arr); 
     
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('htfrmmanagerhotel_divprint', 'innerHTML', $oTbs->Source);
    $objResponse->addScript("$('#htfrmmanagerhotel_divprint').dialog('open')");    
    return $objResponse->getXML();
}

/*
*Hàm tính giá trị theo từng ngày đưa vào 
*/
//
//function ht_calInday($fromhms,$tohms,$room_type_id,$dofw){
//    $arrPrice= array();
//    $hourFrm =    (int)Date('H',strtotime($fromhms));
//    $hourTo =    (int)Date('H',strtotime($tohms));
//    $minuteFrm = (int)Date('i',strtotime($fromhms));
//    $minuteTo =   (int)Date('i',strtotime($tohms));
//    $price=0;
//    $arrPrice = createArrPrice($dofw,$room_type_id);
//    for($i=$hourFrm;$i<=$hourTo;$i++){
//        if($i==$hourFrm){
//            if($i==$hourTo){
//               $price += ($minuteTo-$minuteFrm)*$arrPrice[$i]/60 ; 
//            }else{
//              $price += (60-$minuteFrm)*$arrPrice[$i]/60 ;  
//            }
//            
//        }elseif($i==$hourTo){
//            if($minuteTo==59){
//                $minuteTo=60;
//            }
//            $price += $minuteTo*$arrPrice[$i]/60 ;
//        }else{
//           $price += $arrPrice[$i];
//        }
//    }
//    return round($price,-3);
//}

/*
*hàm trả về tổng giá trị tiền thuê phòng trong khoảng thời gian từ $from đến $to 
* $from : thời gian bắt đầu
* $to : thời gian kết thúc
* $room_type_id : loại phòng
*/
//function ht_calTotal($from,$to,$room_type_id){
//    $total =0;
//    while (strtotime($from) <= strtotime(($to))) 
//    {
//        $dofw = date('w',$from);
        //$arrPrice = $arrPriceAll[$dofw];

//        if(substr($from,0,10)==substr($to,0,10)){            
//          $total+=ht_calInday($from,$to,$room_type_id,$dofw)  ;
//        }else{
//            $totemp = Date('Y-m-d 23:59:59',strtotime($from)) ;            
//            $total+=ka_calInday($from,$totemp,$room_type_id,$dofw)  ;  
//        }
//        $from=Date('Y-m-d 00:00:00', (strtotime( '+1 day' , strtotime($from)))) ;
//    }
//    return $total;
//}

function htfrmmanagerhotel_calRentRoom($arrCurrent,$arrRoomPrice,$startTime,$endTime,$flgCal){
    
    //echo '<pre>'; print_r($arrCurrent);die();
    $timeStart = $arrCurrent[0]['timeStart']; 
    $timeEnd   = $arrCurrent[0]['timeEnd'];
    $timeTotal = $arrCurrent[0]['timeTotal'];
    $maxTimeRange = 5;   // khoảng tối đa thuê theo giờ.
    $moneyperday = $arrRoomPrice['priceDay'];   // số tiền trả trên 1 ngày thuê phòng.
    
    
    $hourStart = substr($timeStart,11,2);
    $hourStart =  (int)$hourStart;
    $hourEnd   =  substr($timeEnd,11,2);
    $hourEnd =  (int)$hourEnd+1;       
    $dayRent = (int)($timeTotal/24) ;
    $hourRent =  $timeTotal%24;  
    $moneyResult = 0;  
   if($flgCal==1){
        if($timeTotal<=$maxTimeRange){
            $timeTotalIndex =  ceil($timeTotal);
             
            $moneyResult = $arrRoomPrice['price'.$timeTotalIndex] ; // tạo ra cái mãng. ứng với từ mấy giờ đến mấy giờ thì giá tiền là bao nhiêu.
        }else{       

        if(($hourStart>=$startTime&&$hourStart<=$endTime)||($hourEnd>=$startTime&&$hourEnd<=$endTime)){
            // trường hợp giờ thuê hoặc giờ trả phòng rơi vào khoảng thời gian checkout

            if($hourRent<=$maxTimeRange){ // trường hợp giờ thuê bị vơi ra nhỏ hơn trong khoảng thuê theo giờ
                $timeTotalIndex =  ceil($hourRent);
                $moneyResult = $arrRoomPrice['price'.$timeTotalIndex] + $dayRent*$moneyperday ;
            }else{
                $moneyResult =  ($dayRent+1)*$moneyperday ; 
            }      
        }else{ // trường hợp giờ thuê hoặc giờ trả phòng không rơi vào khoảng thời gian checkout 
            // if($dayRent==0){// trường hợp thuê dưới 24h
            if(htfrmmanagerhotel_checkSpendTimeOut($hourStart,$hourEnd,$endTime)){
                $timeMin = htfrmmanagerhotel_getMinTime($hourStart,$hourEnd,$startTime,$endTime) ;
                if($timeMin>$maxTimeRange){
                $moneyResult =  $moneyperday*2;
                }else{
                $moneyResult = $arrRoomPrice['price'.$timeMin] + $moneyperday ;  
                }
            }else{
                if($hourEnd-$endTime<=$maxTimeRange){
                $timeMin     = $hourEnd-$endTime;
                $moneyResult = $arrRoomPrice['price'.$timeMin];
                }else{
                $moneyResult = $moneyperday;   
                }

            }
            $moneyResult += $dayRent*$moneyperday ;
            
            }

        }
       
   }else{
      if($hourRent<$maxTimeRange){
          $timeMin = $hourRent+1;  
          $moneyResult = $arrRoomPrice['price'.$timeMin];
      }else{
         $moneyResult = $moneyperday; 
      }
      $moneyResult += $dayRent*$moneyperday ;   
   }
   return $moneyResult; 
    
}

function htfrmmanagerhotel_checkSpendTimeOut($timeStart,$timeEnd,$timeOut){
     
    if($timeStart<$timeOut && ($timeEnd>$timeOut||$timeStart>$timeEnd)|| $timeStart>$timeOut && $timeEnd>$timeOut && $timeEnd<$timestart){
         return true;
     }
     return false;
}
// lấy khoảng thời gian nhỏ nhất từ khoảng thời gian cho phép đến  thời gian 
function htfrmmanagerhotel_getMinTime($timestart,$timeEnd,$timeStartNorm,$timeEndNorm){
      $timeTemp1 = $timeStartNorm  -  $timestart;
      if($timeTemp1<0)$timeTemp1= 24 + $timeTemp1;
      $timeTemp2 = $timeEnd - $timeEndNorm;
      if($timeTemp2<0)$timeTemp2 = 24 + $timeTemp2;
      if($timeTemp1>$timeTemp2){
         return $timeTemp2; 
      }      
      return $timeTemp1;      
}
    
?>
